package com.atguigu.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.baseservice.handler.GuliException;
import com.atguigu.eduService.Listener.SubjectExcelListener;
import com.atguigu.eduService.entity.EduSubject;
import com.atguigu.eduService.entity.vo.ExcelSubjectData;
import com.atguigu.eduService.entity.vo.OneSubjectVo;
import com.atguigu.eduService.entity.vo.TwoSubjectVo;
import com.atguigu.eduService.mapper.EduSubjectMapper;
import com.atguigu.eduService.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-12
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void addSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream
                    , ExcelSubjectData.class
                    , new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(20001, "导入课程分类失败");
        }
    }

    //查询所有课程分类
    @Override
    public List<OneSubjectVo> getAllSubject() {
        //1.查询一级所有分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //2.查询二级所有分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        //ne不等于
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);
        //3.封装一级分类
        List<OneSubjectVo> allSubjectList = new ArrayList<>();
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject oneSubject = oneSubjectList.get(i);
            OneSubjectVo oneSubjectVo = new OneSubjectVo();
            BeanUtils.copyProperties(oneSubject, oneSubjectVo);
            allSubjectList.add(oneSubjectVo);
            //一级分类中封装二级分类
            List<TwoSubjectVo> twoSubjectVos = new ArrayList<>();
            for (int j = 0; j < twoSubjectList.size(); j++) {
                EduSubject twoSubject = twoSubjectList.get(j);
                //二级父id等于一级id
                if (twoSubject.getParentId().equals(oneSubject.getId())) {
                    TwoSubjectVo twoSubjectVo = new TwoSubjectVo();
                    BeanUtils.copyProperties(twoSubject, twoSubjectVo);
                    twoSubjectVos.add(twoSubjectVo);
                }
                oneSubjectVo.setChildren(twoSubjectVos);

            }

        }
        //4.找到跟一级有关的二级进行封装
        return allSubjectList;
    }
}
